---
--类名: BiddingController.lua
--创建日期: 2018-9-10
--创建人: 宋建邦
--功能描述:竞价控制器
--修改内容:
--修改日期:
--修改人:
--修改备注:

local TownInfoView = import("..views.bidding.TownInfoView")
local PriceInfoView = import("..views.bidding.PriceInfoView")
local ChallengeListView = import("..views.bidding.ChallengeListView")
local FirstChellenageTipsView = import("..views.bidding.FirstChellenageTipsView")
local EnterBattleView = import("..views.bidding.EnterBattleView")
local BattleMapModel = import("..models.BattleMapModel")

local fileName = string.getModeName(...)
local BiddingController = class(fileName, app.mvc.ControllerBase)

BiddingController.townlistChange = "bidding.list.change"
BiddingController.chellengeListChange = "bidding.chellenge.list.change"
function BiddingController:ctor(params)
    self.super.ctor(self, params)
end

function BiddingController:init()
    self.worldMapModel = app:getInst("WorldMapModel")

    app.session:on("pb_town.S2C_ChallengeRightList",function(ret)
        dump(ret, "S2C_ChallengeRightList--->")
        self.challengeList = ret
        if #ret.infos > 0 then
            --本公会挑战权列表

            EM:notify(self.chellengeListChange)
            self:addNotify()
        else
            self:removeNotify()
        end
        self:reqTownList()
    end)

    app.session:on("pb_town.S2C_TownWarFirstOpenNotify",function(ret)
        self:addNotify(Constant.FUNC_TYPE_DYNASTY_FIRST)
    end)    

    self.challengeList = self:getChallengeList()
    EM:on("onMapChanged", handler(self, self.onMapLoaded))
    EM:on("S2C_TownWarSettlement", handler(self, self.onTownWarSettlement))
    EM:on(self.townlistChange, handler(self, self.onMapLoaded))
end

function BiddingController:addNotify(rid)
    local params = {
		remindId = Constant.FUNC_TYPE_DYNASTY_CHELLENGE_LIST,
		dataTemp = nil
    }

    if rid then
        params.remindId = rid
    end

    local k = "delayHandler"..tostring(params.remindId)
    if self[k] then
        scheduler.unscheduleGlobal(self[k])
        self[k] = nil
    end

    local desktopController = UIHandler:getDesktopController()
    if desktopController then
		desktopController:insertFourthTips( params )
    else
        self[k] = scheduler.performWithDelayGlobal(function()
            self:addNotify()
            end, 1)
    end
end

function BiddingController:removeNotify()
    local desktopController = UIHandler:getDesktopController()
    if desktopController then
		desktopController:removeFourthTipsByType( Constant.FUNC_TYPE_DYNASTY_CHELLENGE_LIST )
    end
end

function BiddingController:notifyChellengeList()
    if self:getChallengeList() == nil or #self:getChallengeList().infos <= 0 then
        return
    end

    self:openChellengeListView()
    self._chellengeListView:refresh(self:getChallengeList().infos)
end

function BiddingController:notifyFirstChellenge()
    self:openFirstChellenageTipsView()
end

function BiddingController:getChallengeList()
    -- local ret = {}
    -- ret.infos = {
    --     {townId=105103, prosperity=10},
    --     {townId=105101, prosperity=10},
    --     {townId=104101, prosperity=10},
    --     {townId=102102, prosperity=10},
    --     {townId=104102, prosperity=10},
    -- }

    -- return ret
    return self.challengeList
end

function BiddingController:onTownWarSettlement(msg)
    --领主变动，重新请求
    if msg then
        self:reqTownList()
    end
end

function BiddingController:onMapLoaded()
    if self.battleMapModel == nil then
        self:reqTownList()
        return
    end

    self:loadMapFlags()
end

-- 领地场景中插旗
function BiddingController:loadMapFlags()
    NpcFactory:createFlagNpc()
    NpcFactory:setGuildNpcTitles()
end

function BiddingController:canEnterTown(tid)
    if self.challengeList and self.challengeList.infos then
        for i, v in ipairs(self.challengeList.infos) do
            if v.townId == checknumber(tid) then
                return true
            end
        end
    end
end

function BiddingController:hasOwner(tid)
    local ti = self.battleMapModel.hTownInfos[tid]
    return ti.data.owner and ti.data.owner.info and ti.data.owner.info.guildName
end

function BiddingController:isMyTown(tid)
    local myname = self:myGuildName()
    if myname and self:hasOwner(tid) then
        local ti = self.battleMapModel.hTownInfos[tid]
        return myname == ti.data.owner.info.guildName
    end
end

function BiddingController:myGuildName()
    local gm = app:getInst("NewguildModel")
    local ginfo = gm:getMyGuildInfo()
    if ginfo and ginfo:getGuildName() then
        return ginfo:getGuildName()
    end
end

function BiddingController:hasPriceRight()
    local gm = app:getInst("NewguildModel")
    local minfo = gm:getMyGuildMemberInfo()
    if minfo then
        return minfo:isGuildManager()
    end
end

-- function BiddingController:openView()
--     self:reqTownList()
--     if cc.isDead(self._view) then
-- 		self._view = WorldBattleView.new(app.mvc.VIEWTYPE_BLUR)
-- 		self._view:onCloseView(handler(self, self.closeView))
-- 		self._view:onClearView(handler(self, self.clearView))
-- 	end
-- 	self:addChild(self._view)
-- 	self._view:show()
-- 	-- local maps = self._model:getMapInfos()
--     -- self._view:refresh(maps)
    
-- end

-- function BiddingController:clearView()
-- 	if self._view then 
-- 		self._view = nil
-- 	end
-- end

function BiddingController:commonClose(v, cb)
    local view = self[v]
    local this = self
    if view then
        view:onCloseView(function()
            if cb then
                cb()
            end
            -- this:removeChild(view)
            UIHandler:removePopupUiByKey(this[v]:getViewName())
            this[v] = nil
        end)
        view:closeView()
    end
end

function BiddingController:commonOpen(n, c)
    if cc.isDead(self[n]) then
        self[n] = c.new(app.mvc.VIEWTYPE_BLUR)
    end

    UIHandler:addPopupUi(self[n], self[n]:getViewName())
    self[n]:show()
end

function BiddingController:openPriceView(data)
    self:commonOpen("_priceView", PriceInfoView)
end

function BiddingController:closePriceView()
    self:commonClose("_priceView")
end

function BiddingController:openTownInfoView(data)
    self:commonOpen("_townInfoView", TownInfoView)
end

function BiddingController:closeTownInfoView()
    self:commonClose("_townInfoView")
end

function BiddingController:openChellengeListView()
    self:commonOpen("_chellengeListView", ChallengeListView)
end

function BiddingController:closeChellengeListView()
    self:commonClose("_chellengeListView")
end

function BiddingController:openFirstChellenageTipsView()
    self:commonOpen("_firstChellenageTipsView", FirstChellenageTipsView)
end

function BiddingController:closeFirstChellenageTipsView()
    self:commonClose("_firstChellenageTipsView")
end

function BiddingController:openEnterBattleView()
    self:commonOpen("_enterBattleView", EnterBattleView)
end

function BiddingController:closeEnterBattleView()
    self:commonClose("_enterBattleView")
end

function BiddingController:getTownOwnerName(tid)
    if self.battleMapModel then
        local ti = self.battleMapModel.hTownInfos[tid]
        if ti and ti.data and ti.data.owner and ti.data.owner.info then
            return ti.data.owner.info.guildName
        end
    end
end

function BiddingController:enterTown(tid)
    self:openEnterBattleView()
    if self._enterBattleView and self.battleMapModel then
        self._enterBattleView:refresh(self.battleMapModel.hTownInfos[tid])
    end
end

--请求城镇列表
function BiddingController:reqTownList()
    app.session:request("pb_town.C2S_TownList", {}, function(msg)
        local m = BattleMapModel.new()
        m:read(msg)
        self.battleMapModel = m
        if self._view then
            self._view:refresh(m)
        end
        EM:notify(self.townlistChange)
    end, nil, function(errMsg)
    end)
end

function BiddingController:reqPrice(tid, p)
    app.session:request("pb_town.C2S_TownBidding", {id = checknumber(tid), price = p}, function(msg)
        self:reqTownList()
        if msg.ok then
            display.pushToast(L("tips_bidding_success"))
        else
            display.pushToast(L("tips_bidding_fail"))
        end
    end, nil, function(errMsg)
    end)
end

function BiddingController:reqEnterBattle(tid)
    app.session:request("pb_town.C2S_EnterTownWar", {id = checknumber(tid)}, function(msg)
    end, nil, function(errMsg)
    end)
end

return BiddingController